using UnityEngine;
using System.Collections;

public enum PlayerAnimations {
	IDLE = 0,
	IGNITE,
	ONFIRE,
	IDLE_NOGUN,
	IGNITE_NOGUN,
	ONFIRE_NOGUN,	
	EXTINGUISH,
	WALK,
	WALK_NOGUN,
	NUM_ANIMS
}

public class Player : MonoBehaviour {

	public float SPEED = 30.0f;

	public float curSpeed;
	public CharacterAnimation[] _animations;
	public CharacterAnimator _animator;

	public bool isOnFire;
	public bool hasGun;

	int _maxHealth;
	int _curHealth;
	float _damageTime;
	float _damageTimer;
	bool _isIgniting;
	bool _isExtinguishing;
	GameManager _gameManager;

	// Use this for initialization
	void Start () {
		_animator = GetComponent<CharacterAnimator> ();

		curSpeed = SPEED;
		isOnFire = false;
		_isIgniting = false;
		_isExtinguishing = false;

		_maxHealth = 100;
		_curHealth = _maxHealth;

		_damageTime = 0.1f;
		_damageTimer = 0.0f;


		Idle ();
	}
	
	// Update is called once per frame
	void Update () {
		CheckInput ();

		if (!_animator.isPlaying) {
			if(_isIgniting) {
				Enflame();
			} else if (!isOnFire || _isExtinguishing) {
				Idle ();
			}
		}

		if (isOnFire) {
			_damageTimer += Time.deltaTime;
			if(_damageTimer >= _damageTime) {
				--_curHealth;
				_damageTimer = 0.0f;
				CheckDeath();
			}
		}
	}

	void CheckInput() {
		if (Input.GetKeyDown (KeyCode.Space)) {
			if(isOnFire) {
				if(hasGun) {
					// Put out fire
					Extinguish();
				}
			} else {
				// Light on fire
				Ignite();
			}
		} else if (Input.GetKeyUp (KeyCode.Space)) {
			if(_isIgniting) {
				Idle ();
			}
		}

		if (!_isIgniting && !_isExtinguishing) {
			float x = 0.0f;
			float y = 0.0f;

			if (Input.GetKey (KeyCode.UpArrow)) {
				y = curSpeed;
				Walk(false);
			} else if (Input.GetKey (KeyCode.DownArrow)) {
				y = -curSpeed;
				Walk(false);
			}

			if (Input.GetKey (KeyCode.LeftArrow)) {
				x = -curSpeed;
				Walk(false);
			} else if (Input.GetKey (KeyCode.RightArrow)) {
				x = curSpeed;
				Walk(true);
			}

			rigidbody2D.velocity = new Vector2 (x, y);

			if(x == 0 && y == 0) {
				Idle();
			}
		} else {
			rigidbody2D.velocity = new Vector2 (0, 0);
		}
	}

	void CheckDeath() {
		if (_curHealth <= 0) {
			_curHealth = 0;
			// Die
			DestroyObject(this.gameObject);
		}
	}

	void Idle() {
		if (hasGun) {
			if(isOnFire) {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.ONFIRE], 0);
			} else {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.IDLE], 0);
			}
		} else {
			if(isOnFire) {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.ONFIRE_NOGUN], 0);
			} else {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.IDLE_NOGUN], 0);
			}
		}

		_isIgniting = false;
		_isExtinguishing = false;
	}

	void Walk(bool walkRight) {
		if(!_isIgniting && !isOnFire) {
			if(hasGun) {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.WALK], 0, walkRight);
			} else {
				_animator.ChangeAnimation(_animations[(int)PlayerAnimations.WALK_NOGUN], 0, walkRight);
			}
		}
	}

	void Ignite() {
		if(hasGun) {
			_animator.ChangeAnimation(_animations[(int)PlayerAnimations.IGNITE], 0);
		} else {
			_animator.ChangeAnimation(_animations[(int)PlayerAnimations.IGNITE_NOGUN], 0);
		}
		_isIgniting = true;
	}

	void Enflame() {
		curSpeed = SPEED * 2;
		isOnFire = true;
		_isIgniting = false;
	}
	
	void Extinguish() {
		_animator.ChangeAnimation(_animations[(int)PlayerAnimations.EXTINGUISH], 0);
		curSpeed = SPEED;
		isOnFire = false;
		_isExtinguishing = true;
	}

	void OnCollisionEnter2D(Collision2D collision) {
		if (collision.collider.tag == "Door") {
			if(collision.collider.GetComponent<Door>()._isOpen) {
				// Tell GameManager to load next level
				_gameManager.LoadNextRoom();
			}
		}
	}

	void OnTriggerEnter2D(Collider2D other) {
		if(other.tag == "Water") {
			if (isOnFire) {
				Extinguish();
			}
		}
	}
	
	void OnTriggerStay2D(Collider2D other) {
		if (other.tag == "FireTile" && !_isIgniting && !isOnFire) {
			Enflame();
		}
		if (other.tag == "TikiAttack") {
			if(other.GetComponent<AttackHitbox>().attacking) {
				_curHealth -= 10;
				other.GetComponent<AttackHitbox>().attacking = false;
				CheckDeath();
			}
		}
	}

	void OnGUI() {
		GUI.Box (new Rect (1.0f, 5.0f, _curHealth, 10.0f), _curHealth.ToString());
	}

	void OnLevelWasLoaded(int level) {
		_gameManager = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<GameManager>();

		if (_gameManager.roomType == ROOMTYPES.PUZZLE) {
			LoseGun();
		} else {
			GainGun();
		}
	}

	public void GainGun() {
		hasGun = true;
	}

	public void LoseGun() {
		hasGun = false;
	}
}
